﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Eventing.Reader;

namespace org.swinchester.windowsevents.core
{
    public class EventQueryReader : EventReaderBase
    {
        //public EventSubscriptionConfig SubscriptionConfig { get; set; }

        public event GenericEventRecordHandler EventQueryRecordRead;

        //public delegate void GenericEventRecordHandler(EventRecord evtRecord, EventArgs args);

        public void Exhaust()
        {
            EventLogReader elr = null;
            try
            {
                //query the event log > last event in DB
                EventLogQuery elq = new EventLogQuery(SubscriptionConfig.EventLogName, SubscriptionConfig.EventPathType, SubscriptionConfig.EventLogQuery);
                //run query
                elr = new EventLogReader(elq);

                //send all of those events.
                for (EventRecord eventInstance = elr.ReadEvent(); null != eventInstance; eventInstance = elr.ReadEvent())
                {
                    EventQueryRecordRead(eventInstance, null);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                elr.Dispose();
            }
        }
    }
}
